package com.cloudptt.wp.ui.message.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.res.Resource;
import com.cloudptt.wp.R;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.SessionInfo;
import com.mcptt.db.model.SessionReadStatus;

import java.util.List;

public class MessageStatusAdapter extends BaseAdapter {

    private Context ctx = null;
    private List<SessionReadStatus> readStatusesList;
    private SessionInfo sessionInfo = null;

    public MessageStatusAdapter(Context ctx,SessionInfo sessionInfo,List<SessionReadStatus> readStatusesList) {
        this.ctx = ctx;
        this.sessionInfo = sessionInfo;
        this.readStatusesList = readStatusesList;
        Log.debug(this.getClass()," MessageStatusAdapter readStatusesList = " + readStatusesList);
    }


    @Override
    public int getCount() {
        if(readStatusesList != null){
            return readStatusesList.size();
        }else{
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return readStatusesList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MessageStatusAdapter.ViewHolder viewHolder;
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            convertView = inflater.inflate(R.layout.activity_read_status_item, null);

            viewHolder = new MessageStatusAdapter.ViewHolder();
            viewHolder.nameTv = (TextView) convertView.findViewById(R.id.nameTv);
            viewHolder.statusTv = (TextView) convertView.findViewById(R.id.statusTv);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (MessageStatusAdapter.ViewHolder) convertView.getTag();
        }
        SessionReadStatus info = readStatusesList.get(position);
        String descStr = info.getDscp();
        viewHolder.nameTv.setText(descStr);
        int status = info.getStatus();
        String val = Resource.getMcdtataStatusString(sessionInfo.isFdMsg(),status);
        Log.debug(getClass(), "status = " + status + " val = " + val);
        viewHolder.statusTv.setText(val);
        return convertView;
    }

    public void setMemList(List<SessionReadStatus> readStatusesList) {
        this.readStatusesList = readStatusesList;
    }

    private class ViewHolder {
        public TextView nameTv;
        public TextView statusTv;
    }
}
